package com.sunyur.tailor.channel.web.outer.file.controller;

import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.util.ChannelResultUtil;
import com.sunyur.tailor.channel.service.inner.file.model.FileDownloadModel;
import com.sunyur.tailor.channel.service.inner.file.param.FileDownloadParam;
import com.sunyur.tailor.channel.service.inner.file.service.FileService;
import com.sunyur.tailor.channel.web.outer.file.dto.FileDownloadDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

/**
 * @Author: HuBaoding
 * @Description:
 * @Date: Created in 17:39 2021/8/9
 */
@RestController
@RequestMapping("/outer/file")
public class FileController {

    @Autowired
    private FileService fileService;
    /**
     * 文件下载
     * @param dto
     * @return
     */
    @PostMapping("/download")
    public ChannelResult<byte[]> fileDownload(@Valid @RequestBody FileDownloadDTO dto){
        FileDownloadParam fileDownloadParam=new FileDownloadParam();
        BeanUtils.copyProperties(dto,fileDownloadParam);
        ChannelResult<FileDownloadModel> channelResult= fileService.download(fileDownloadParam);
        if(ChannelResultUtil.isSuccess(channelResult)){
            return ChannelResultUtil.success(channelResult.getContent().getFileContent());
        }
        return ChannelResultUtil.error(channelResult.getCode(),channelResult.getMessage());
    }

}
